package com.wei.frame.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.wei.frame.App;
import com.wei.frame.di.component.AppComponent;
import com.wei.frame.framework.BasePresenter;
import com.wei.frame.framework.IView;
import com.wei.frame.ui.lifecycle.FragmentEvent;
import com.wei.frame.ui.lifecycle.LifecycleProvider;
import com.wei.frame.ui.lifecycle.LifecycleTransformer;
import com.wei.frame.ui.lifecycle.RxLifecycle;
import com.wei.frame.ui.lifecycle.RxLifecycleAndroid;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

/**
 * 作者：赵若位
 * 时间：2020/8/20 2:31
 * 邮箱：1070138445@qq.com
 * 功能：
 */
//TODO 添加Fragment的状态栏变色
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements IView, LifecycleProvider<FragmentEvent> {
    protected final String TAG = this.getClass().getSimpleName();

    private final BehaviorSubject<FragmentEvent> mLifecycleSubject = BehaviorSubject.create();

    abstract int getLayoutId();

    abstract void bindComponent(AppComponent component);

    abstract void initView(Bundle savedInstanceState);

    abstract void initData();

    private P mPresenter;
    private View mRootView;
    private Unbinder mUnbinder;


    @Override
    @NonNull
    @CheckResult
    public final Observable<FragmentEvent> lifecycle() {
        return mLifecycleSubject.hide();
    }

    @Override
    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull FragmentEvent event) {
        return RxLifecycle.bindUntilEvent(mLifecycleSubject, event);
    }

    @Override
    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycleAndroid.bindFragment(mLifecycleSubject);
    }


    @Override
    public void onAttach(android.app.Activity activity) {
        super.onAttach(activity);
        mLifecycleSubject.onNext(FragmentEvent.ATTACH);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLifecycleSubject.onNext(FragmentEvent.CREATE);
        bindComponent(App.getComponent());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == mRootView) {
            mRootView = getLayoutId() > 0 ? inflater.inflate(getLayoutId(), container, false) : null;
            mUnbinder = ButterKnife.bind(this, mRootView);
        }
        initView(savedInstanceState);
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mLifecycleSubject.onNext(FragmentEvent.CREATE_VIEW);
        initData();
    }

    @Override
    public void onStart() {
        super.onStart();
        mLifecycleSubject.onNext(FragmentEvent.START);
    }

    @Override
    public void onResume() {
        super.onResume();
        mLifecycleSubject.onNext(FragmentEvent.RESUME);
    }

    @Override
    public void onPause() {
        mLifecycleSubject.onNext(FragmentEvent.PAUSE);
        super.onPause();
    }

    @Override
    public void onStop() {
        mLifecycleSubject.onNext(FragmentEvent.STOP);
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        mLifecycleSubject.onNext(FragmentEvent.DESTROY_VIEW);
        super.onDestroyView();
        if (null != mUnbinder && mUnbinder != Unbinder.EMPTY) {
            mUnbinder.unbind();
            mUnbinder = null;
        }
    }

    @Override
    public void onDestroy() {
        mLifecycleSubject.onNext(FragmentEvent.DESTROY);
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        mLifecycleSubject.onNext(FragmentEvent.DETACH);
        super.onDetach();
    }


    @Override
    public View getRootView() {
        return mRootView;
    }

    @Override
    public void alert(@Nullable String alert) {

    }

    @Override
    public void log(@Nullable String errorMessge) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showDialog(@Nullable String message) {

    }

    @Override
    public void openActivity(@Nullable Class<? extends Activity> cls) {
        startActivity(new Intent(getActivity(),cls));
    }
}
